/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAO.CollectionDAO;
import DAO.TrackedProductDAO;
import hibernate.entity.Account;
import hibernate.entity.Collection;
import hibernate.entity.TrackedProduct;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tùng
 */
public class CollectionController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String url = "CollectionController?btAction=LoadAllCollection";
        try {
            String action = request.getParameter("btAction");
            if (action.equals("LoadAllCollection")) {
                CollectionDAO colDAO = new CollectionDAO();
                Account acc = (Account) session.getAttribute("ACCOUNT");
                if (acc != null) {
                    List<Collection> result = colDAO.getCollectionsbyAccount_Id(acc.getAccountID());
                    session.setAttribute("SHOWCOLLECTION", result);
                    response.sendRedirect(Common.Global.collectionpage);
                } else if (acc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            } else if (action.equals("showCollectionDetail")) {
                session.removeAttribute("EDITCOLID");
                session.removeAttribute("EDITCOLNAME");
                session.removeAttribute("EDITCOLDES");
                TrackedProductDAO tpDAO = new TrackedProductDAO();
                Account acc = (Account) session.getAttribute("ACCOUNT");
                if (acc != null) {
                    int collectionID = Integer.parseInt(request.getParameter("collectionID"));
                    CollectionDAO colDAO = new CollectionDAO();
                    Collection col = colDAO.getCollectionByID(collectionID);
                    String colName = col.getCollectionName();
                    List<TrackedProduct> result = tpDAO.getTrackedProductbyCollection(acc.getAccountID(), collectionID);
                    if (!result.isEmpty()) {
                        session.removeAttribute("SHOWLCOLDETAILERROR");
                        session.setAttribute("SHOWPRODUCTCOLLECTION", result);
                        session.setAttribute("SHOWCOLLECTIONNAME", colName);
                    } else {
                        session.removeAttribute("SHOWPRODUCTCOLLECTION");
                        session.setAttribute("SHOWLCOLDETAILERROR", "There are't any products in " + colName + " collection!!!");
                    }
                    response.sendRedirect(Common.Global.collectionpage);
                } else if (acc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            } else if (action.equals("CreateCollection")) {
                Account getAcc = (Account) session.getAttribute("ACCOUNT");
                int accID = getAcc.getAccountID();
                String collectionName = request.getParameter("txtCollectionName");
                String description = request.getParameter("txtCollectionDescription");
                CollectionDAO colDAO = new CollectionDAO();
                boolean check = colDAO.createCollection(accID, collectionName, description);
                if (check) {
                    session.setAttribute("CREATECOLSUC", "You created collection " + collectionName + " successfully!!!");
                } else {
                    session.setAttribute("CREATECOLERR", "You created collection " + collectionName + " failed!!!");
                }
                response.sendRedirect(url);
            } else if (action.equals("removeItem")) {
                session.removeAttribute("SHOWPRODUCTCOLLECTION");
                int trackedProductID = Integer.parseInt(request.getParameter("removeCollectionID"));
                CollectionDAO colDAO = new CollectionDAO();
                boolean check = colDAO.removeItem(trackedProductID);
                if (check) {
                    session.setAttribute("REMOVEITEMSUC", "Product is removed from collection!!!");
                } else {
                    session.setAttribute("REMOVEITEMERR", "Product can't be removed from collection!!!");
                }
                response.sendRedirect(url);
            } else if (action.equals("createNewCol")) {
                session.removeAttribute("EDITCOLID");
                session.setAttribute("CREATENEWCOL", out);
                response.sendRedirect(Common.Global.collectionpage);
            } else if (action.equals("editCol")) {
                session.removeAttribute("SHOWPRODUCTCOLLECTION");
                String editColID = request.getParameter("editColID");
                session.setAttribute("EDITCOLID", editColID);
                String editColName = request.getParameter("editColName");
                session.setAttribute("EDITCOLNAME", editColName);
                String editColDes = request.getParameter("editColDes");
                session.setAttribute("EDITCOLDES", editColDes);
                response.sendRedirect(Common.Global.collectionpage);
            } else if (action.equals("CloseCol")) {
                session.removeAttribute("EDITCOLID");
                session.removeAttribute("EDITCOLNAME");
                session.removeAttribute("EDITCOLDES");
                response.sendRedirect(url);
            } else if (action.equals("saveCollection")) {
                int colID = Integer.parseInt(request.getParameter("txtColID"));
                String colName = request.getParameter("txtEditColName");
                String colDes = request.getParameter("txtEditColDes");
                CollectionDAO colDAO = new CollectionDAO();
                boolean check = colDAO.updateCollection(colName, colDes, colID);
                if (check) {
                    session.setAttribute("EDITCOLSUCCESS", "You updated collection " + colName + " successfully");
                    session.removeAttribute("EDITCOLID");
                } else {
                    session.setAttribute("EDITCOLERROR", "You updated collection " + colName + " failed");
                }
                response.sendRedirect(url);
            } else if (action.equals("deleteCollection")) {
                TrackedProductDAO tpDAO = new TrackedProductDAO();
                Account acc = (Account) session.getAttribute("ACCOUNT");
                if (acc != null) {
                    int collectionID = Integer.parseInt(request.getParameter("delColID"));
                    tpDAO.removeProductFromCollection(collectionID);
                    CollectionDAO colDAO = new CollectionDAO();
                    Collection col = colDAO.getCollectionByID(collectionID);
                    String colName = col.getCollectionName();
                    boolean check = colDAO.deleteCollection(collectionID);
                    if (check) {
                        session.setAttribute("DELETECOLSUC", "You deleted collection " + colName + " successfully!!!");
                    } else {
                        session.setAttribute("DELETECOLERROR", "You deleted collection " + colName + " failed!!!");
                    }
                    response.sendRedirect(url);
                } else if (acc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
